<template>
	<div>
		<div class="h2">问卷范围列表</div>
		<div id="components-form-demo-advanced-search" style="display: none;">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="部门">
							<a-select :allowClear="true" v-decorator="['departCode']" placeholder="请选择部门" optionFilterProp="children" >
								<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="年级">
							<a-date-picker mode="year" format="YYYY" :open="isopen" @change="onChange"
								@openChange="handYearChange" @panelChange="panelYearChange" v-decorator="['year']"
								placeholder="选择年级" />
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="班级">
							<a-select
								v-decorator="['classID']"
								showSearch
								optionFilterProp="children"
								style="width: 100%"
								:showArrow="false"
								@search="fetchClub"
								:filterOption="filterOption"
								placeholder="请输入关键字进行搜索"
								allowClear
							>
								<a-select-option v-for="(item, index) in ClassList" :key="item.ID">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="俱乐部">
							<a-select v-decorator="['clubID']" showSearch optionFilterProp="children"
								style="width: 100%" :showArrow="false" @search="fetchClubs" :filterOption="filterOption"
								placeholder="请输入关键字进行搜索" allowClear>
								<a-select-option v-for="(item, index) in ClubList" :key="item.ID">{{ item.Name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="8" style="margin-top:4px;">
						<a-button type="primary" html-type="submit" class="btn1">搜索</a-button>
						<a-button :style="{ marginLeft: '8px' }" @click="handleReset" class="btn2">重置</a-button>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<div class="option">
			<a-button type="primary" class="btn" @click="seeList('','add')">新增</a-button>
		</div>
		<div style="margin-top:10px;">
			<el-table :data="data" v-loading="loading" border :max-height="tableHeight" highlight-current-row
				style="width: 100%;">
				<el-table-column type="index" label="序号" width="60" fixed="left"></el-table-column>
				<el-table-column prop="DepartName" label="部门" min-width="140"></el-table-column>
				<el-table-column prop="Grade" label="年级" min-width="140" v-if="data.length>0?data[0].UserType==1:false"></el-table-column>
				<el-table-column prop="Value" label="类型" min-width="120">
					<template slot-scope="scope">
						<a-tag color="#108ee9" v-if="scope.row.UserType!=null">{{scope.row.UserType=='1'?'学生':scope.row.UserType=='2'?'老师':''}} </a-tag>
					</template>
				</el-table-column>
			<!-- 	<el-table-column prop="AddTime" label="添加时间" min-width="140"></el-table-column> -->
				<el-table-column label="操作" width="120" fixed="right">
					<template slot-scope="scope">
						<i title="修改" class="iconfont icon-editor1" style="color: #1890FF;"
							@click="editfun(scope.row)"></i>

						<i title="删除" class="iconfont icon-delete" style="color: red;"
							@click="deleteList(scope.row, 3)"></i>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<addMember :DepartCodeList="DepartCodeList" :text="text" v-show="addMemberIf" @closeFun="closeFun"></addMember>
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
		 :current-page="pagination.currentPage" :page-sizes="pagination.pageSizeOptions"
			:page-size="pagination.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total">
		</el-pagination>
	</div>
</template>

<script>
	import moment from 'moment';
	import addMember from './addMember';
	import Func from '@/utils/func';
	import Cache from '@/utils/cache';
	import {
		ManageEvaluationRanageDoGetPageList,
		DeleteActivityMember,
		DoAddActivityManager,
		DoDeleteActivityManager,
		ManageEvaluationRanageDoDelete,
		GetDepartList,
		GetClassList,
		GetClubPageListCommon
	} from '@/api/follow';
	const data = [];
	export default {
		components: {
			addMember
		},
		data() {
			return {
				data,
				ClubList:[],
				ClassList:[],
				DepartCodeList:[],
				addIf: false,
				form: this.$form.createForm(this, {
					name: 'advanced_search'
				}),
				IsAll: this.$route.query.IsAll,
				text: {},
				loading: false,
				tableHeight: parseFloat(window.innerHeight - 160),
				requestUrl: this.global.requestUrl,
				token: Cache.get('token'),
				addMemberIf: false,
				pagination: {
					currentPage: 1,
					pageSize: this.global.pageSize,
					pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
					showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
				},
				values: "",
				Year: null, 
				isopen:false
			};
		},
		computed: {},
		created() {
			this.getList();
			 this.getOtherList1();
		},
		mounted() {},
		methods: {
			async fetchClubs(value) {
				let myReg = /^[^a-zA-Z]+$/;
				if (value) {
					let data = {};
					data.key = value;
					let res = await GetClubPageListCommon(data);
					this.ClubList = res.data.data;
				}
			},
			editfun(text) {
				this.text = text
				this.addMemberIf = true
			},
			onChange() {
				this.createValue = null;
				This.Year = null;
			},
			handYearChange(open) {
				if (open) {
					this.isopen = true
				} else {
					this.isopen = false
				}
			},
			filterOption(input, option) {
				return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
			},
			panelYearChange(value) {
				this.createValue = value;
				this.Year = moment(value).format('YYYY'); //接收panelChange返回的值 并用monent格式化时间为年
				this.form.setFieldsValue({
				    year: this.Year,
				})
				this.isopen = false;
			},
			async fetchClub(value) {
				let myReg = /^[^a-zA-Z]+$/;
				if (value && myReg.test(value)) {
					let data = {};
					data.key = value;
					let res = await GetClassList(data);
					this.ClassList = res.data.data;
				}
			},
			async getOtherList1() {
				let res1 = await GetDepartList();
				this.DepartCodeList = res1.data.data;
				
			},
			moment,
			handleSearch(e) {
				e.preventDefault();
				this.form.validateFields((error, values) => {
					this.values = values;
					this.pagination.currentPage = 1;
					this.getList();
				});
			},
			handleReset() {
				this.form.resetFields();
				this.pagination.currentPage = 1;
				this.values = {};
				this.getList();
			},
			handleSizeChange(val) {
				this.pagination.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.pagination.currentPage = val;
				this.getList();
			},
			async getList() {
				this.loading = true;
				let data = {};
				if (this.values) {
					data = this.values;
				}
				data.evaluationID = this.$route.query.ID ? this.$route.query.ID : '';
				data.pageIndex = this.pagination.currentPage;
				data.pageSize = this.pagination.pageSize;
				let res = await ManageEvaluationRanageDoGetPageList(data);
				if (res.data.code !== 0) {
					this.$message.error(res.data.msg)
				}
				this.loading = false;
				this.data = res.data.data;
				const pagination = {
					...this.pagination
				};
				pagination.total = res.data.pageInfo.TotalItemCount;
				this.pagination = pagination;
			},
			async getOtherList() {
				let res = await GetActivityStateList();
				this.ActivityStateList = res.data.data;
				let res1 = await GetDepartList();
				this.DepartCodeList = res1.data.data;
			},
			deleteList(text, type) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: `您确定删除该问卷范围吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						// data.id = type==2?text.ManagerID:text.ID;
						// data.ActivityID = that.$route.query.ID
						// data.UserNo = text.UserNo
						data.id = text.ID
						let res = await ManageEvaluationRanageDoDelete(data);
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			seeList(text, name) {
				if (name == 'add') {
					// this.text = {time:new Date(),ActivityID:this.$route.query.ID,...text}
					// console.log(this.text)
					this.text = {}
					this.addMemberIf = true
				} else {
					this.$router.push({
						name: name,
						query: {
							ID: text.ID,
							eliteID: this.$route.query.ID
						}
					});
				}
			},
			closeFun(data) {
				console.log(data)
				this.addMemberIf = false;
				this.getList();
			}
		}
	};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		display: flex;
		margin-bottom: 10px !important;
	}

	/deep/.ant-form-item-label {
		width: 80px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	/deep/.el-table th.is-leaf {
		background: #fafafa;
	}

	/deep/.el-table td,
	/deep/.el-table th {
		padding: 5px;
	}

	.option {
		margin-top: 10px;
		margin-bottom: 20px;
		overflow: hidden;
	}

	.option .btn {
		height: 36px;
		line-height: 36px;
		padding: 0 10px;
		float: left;
		background: #1890ff;
		color: #fff;
		margin-right: 10px;
		border-radius: 3px;
		cursor: pointer;
		border: none;
	}

	.foot {
		text-align: center;
		margin-top: 100px;
	}

	.el-pagination {
		margin-top: 10px;
	}
</style>
